package com.st.vmall.controller.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.st.vmall.entity.JSONResult;
import com.st.vmall.entity.Type;
import com.st.vmall.service.ITypeService;

@Controller("adminTypeController")
@RequestMapping("/admin/type")
public class TypeController {
	
	@Autowired
	private ITypeService typeService;

	/**
	 * 页面跳转
	 * @return
	 */
	@GetMapping("/add.do")
	public String add() {
		return "admin/type/add";
	}
	@GetMapping("/index.do")
	public String index() {
		return "admin/type/index";
	}
	
	
	@PostMapping("/add.json")
	@ResponseBody
	public JSONResult add(Type type) {
		//1.参数
		//2.业务
		int res = typeService.add(type);
		
		JSONResult result = new JSONResult();
		result.setCode(res);
		if(res > 0) {
			result.setMessage("添加成功");
		}else {
			result.setMessage("添加失败");
		}
		
		return result;
	}
	
	/**
	 * 获取一级分类
	 */
	@PostMapping("/getFirst.json")
	@ResponseBody
	public JSONResult getFirst() {
		
		List<Type> firstTypes = typeService.getFirst();
		
		JSONResult result = new JSONResult();
		result.setCode(1);
		result.setData(firstTypes);
		
		return result;
	}
	
	@PostMapping("/getSecond.json")
	@ResponseBody
	public JSONResult getSecond(Integer parent) {
		
		List<Type> secondTypes = typeService.getSecond(parent);
		
		JSONResult result = new JSONResult();
		result.setCode(1);
		result.setData(secondTypes);
		
		return result;
	}
	
	@GetMapping("/search.json")
	@ResponseBody
	public JSONResult search(Type type,Integer limit,Integer page) {
		
		PageInfo types = typeService.search(type,page,limit);
		
		JSONResult result = new JSONResult();
		result.setCount(types.getTotal());
		result.setData(types.getList());
		
		return result;
	}
	
	@PostMapping("/delete.json")
	@ResponseBody
	public JSONResult delete(Integer id) {
		
		int res = typeService.delete(id);
		
		JSONResult result = new JSONResult();
		result.setCode(res);
		if(res > 0) {
			result.setMessage("删除成功");
		}else {
			result.setMessage("删除失败");
		}
		
		return result;
	}
	
	@PostMapping("/edit.json")
	@ResponseBody
	public JSONResult edit(Type type) {
		
		int res = typeService.edit(type);
		
		JSONResult result = new JSONResult();
		result.setCode(res);
		if(res > 0) {
			result.setMessage("修改成功");
		}else {
			result.setMessage("修改失败");
		}
		
		return result;
	}
}
